// ---------------------------------------------------------------
//                    flight002.pde
//                     Jan 13 2009
//                  written by elekid
//
// Test program for self-study
// 
// Pocessing official site - http://processing.org
// ---------------------------------------------------------------

// define constants
static final int FR_SEC = 16;  // frames per second
static final int FR_DAY = 4;  // frames per one day

static final int TOKYO=0;
static final int KANAZAWA=1;
static final int HIROSHIMA=2;
static final int FUKUOKA=3;

// define grobal variables
PImage MapImage;
PGraphics pg;
PFont labelFont;

locPoint[]  loc;
Travel[] travel;

int day = 0 ; // count up when dayfremae >= FR_DAY
int pt = 0 ; // counter for array travel
int dayframe = 0 ; // count up each time draw() called and reset when dayfremae >= FR_DAY
int MaxNumTravels = 0 ;
int MaxNumDays = 0 ;
int [][] pathCounter={ 
  {
    0,0,0,0  }
  ,{
    0,0,0,0  }
  ,{
    0,0,0,0  }
  ,{
    0,0,0,0  }
};

int nt;



/**
 * called from Processing environment
 */
void setup()
{
  String[] lines;
  size(727,649);
  pg = createGraphics (727,649,P2D);
  pg.beginDraw();
  pg.background(0,0);
  pg.endDraw();

  //smooth();
  //  print(atan2(1,1));

  MapImage = loadImage("JPN.png");

  labelFont=createFont("SansSerif", 20);
  textFont(labelFont);

  loadDataFile("travel_data.txt"); 

  // HND(TOKYO)    505,390
  // KMQ(KOMATSU)  367,342
  // FKU(FUKUOKA)  82,506  
  // HIJ(HIROSIMA)  174,464
  loc = new locPoint[4];
  loc[TOKYO]     = new locPoint(50,390);
  loc[KANAZAWA]  = new locPoint(367,342);
  loc[HIROSHIMA] = new locPoint(82,506);
  loc[FUKUOKA]   = new locPoint(174,464);

  // DEBUG
  frameRate(FR_SEC);
  println("MaxNumTravels" + MaxNumTravels);
  println("MaxNumDays   " + MaxNumDays);

}

/**
 * called from Processing environment
 */
void draw()
{
  // DEBUG
  // println("day=" + day + ":pt=" + pt + " dayframe=" + dayframe );

  image(MapImage, 0, 0);
  flightControl();
  image(pg, 0, 0);

  //drawData();

  ellipseMode(CENTER);

  int i;
  textSize(20);
  fill(255,0,0);
  text ("MouseX :" + mouseX,550,600);
  text ("MouseY :" + mouseY,550,620);

  if (day < MaxNumDays) {
    dayframe++;
    if (dayframe >= FR_DAY) {
      day++;
      dayframe = 0 ;
      // DEBUG
      println("day++=" + day);
      println("   pt=" + pt);
    }
  }
}

/**
 * called from draw
 */
void flightControl() {

  // test
  //Travel t = new Travel();
  //t.hasNext = false;

  if (day >= MaxNumDays) return;
  if ( dayframe ==0 ) {
    // how many object should we move?
    if ( travel[pt].numMoving == 0 ) {
      pt++;
      nt = 0;
    } 
    else {
      nt = 1;
      while ( travel[pt].hasNext ) {
        nt++ ;
        pt++;
      }
      pt++;
    }
    // DEBUG
    println("travels=" + nt);
    
    // count up go / back 
    for ( int i = (pt - nt) ; i < pt ; i++){
      pathCounter[travel[i].dst][travel[i].src]++;
      pathCounter[travel[i].src][travel[i].dst]++;
    }
  }

  // DEBUG
  for ( int i = (pt - nt) ; i < pt ; i++){
    drawPath(travel[i].src, travel[i].dst, dayframe, FR_DAY);
  }

}

/**
 * draw flight path
 */
 void drawPath(int src, int dst, int step, int max ){
   // DEBUG
   println("--->travel from " + src + " to " + dst + " step " + step + " (Total:"+pathCounter[src][dst]+" times)");

 }



/**
 * read travel data file and store
 */
void loadDataFile(String fileName) {
  int cnt =0,days=0 ; 
  String prev_mm="",prev_dd="";
  int prev_time=0;
  String lines[] = loadStrings(fileName);

  travel = new Travel[lines.length];
  for ( int i=0; i < lines.length; i++){
    if (trim(lines[i]).length() == 0) {
      continue;
    } 
    else if ( lines[i].startsWith("#") ) {
      continue;
    }
    String[] pieces = split(lines[i], ",");
    travel[cnt] = new Travel ( pieces[0], pieces[1], pieces[2], 
    pieces[3], pieces[4], pieces[5], pieces[6], pieces[7], 
    pieces[8], pieces[9], pieces[10], pieces[11], pieces[12]);

    if (( !prev_mm.equals(travel[cnt].mm) ) || 
        ( !prev_dd.equals(travel[cnt].dd) ) || 
        ( prev_time != travel[cnt].time) )
      {
      days++;
      println("     dd:"+travel[cnt].dd);
      println("   days:"+days);

    }
    prev_mm=travel[cnt].mm;
    prev_dd=travel[cnt].dd;
    prev_time=travel[cnt].time;

    cnt++;

  }
  MaxNumTravels = cnt;
  MaxNumDays = days;
  // DEBUG
  println("cnt=" + cnt);
  println("days=" + days);
  for (int i =0; i < MaxNumTravels ; i++){
    travel[i].Dump();
  }
  return;

}  
// ---------------------------------------------------------------
//  class locPoint
// 
// ---------------------------------------------------------------
class locPoint {
  int x,y;

  locPoint(int _x, int _y) {
    x = _x;
    y = _y;
  }
  int getX(){
    return x;
  }
  int getY(){
    return y;
  }
}

// ---------------------------------------------------------------
//  class Travel
// 
// ---------------------------------------------------------------
class Travel {
  String yy;
  String mm;
  String dd;
  String week;
  int time;
  int src=0;
  int dst=0;
  int numMoving=0;
  int numLoc0=0;
  int numLoc1=0;
  int numLoc2=0;
  int numLoc3=0;
  boolean hasNext=false;

  Travel(String _yy, String _mm, String _dd, String _week, String _time, String _src, 
  String _dst, String _numMoving, String _hasNext, 
  String _numLoc0, String _numLoc1, String _numLoc2, String _numLoc3){

    yy = _yy ;
    mm = _mm ;
    dd = _dd ;
    week = _week ;
    time =parseInt(_time) ; 
    src = parseInt(_src);
    dst = parseInt(_dst);
    numMoving = parseInt (_numMoving);
    numLoc0 = parseInt(_numLoc0);
    numLoc1 = parseInt(_numLoc1);
    numLoc2 = parseInt(_numLoc2);
    numLoc3 = parseInt(_numLoc3);

    if ( parseInt(_hasNext) == 1) {
      hasNext=true;
    }
    //println(this.numMoving);

  }
  void Dump(){
    System.out.printf("%s/%s/%s(%s),time=%d,%d to %d(%d),Loc1=%d,Loc2=%d,Loc3=%d,Loc4=%d\n",
    yy, mm,dd,week,time,src,dst,numMoving,numLoc0,numLoc1,numLoc2,numLoc3);

    return;
  }
}






